package com.gzsxy.esjy.service.netty.controller;

import com.gzsxy.esjy.mapping.annotation.Controller;

import com.gzsxy.esjy.mapping.annotation.RequestMapping;
import com.gzsxy.esjy.mapping.annotation.enums.RequestMethod;
import com.gzsxy.esjy.request.HttpServletRequest;
import com.gzsxy.esjy.response.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * api
 * @author zhangxiaolong
 * @date 2022/7/11 9:36
 */
@Controller
@Slf4j
public class ApiController {
	
	private final static ResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
	
	@Value("${spring.cloud.client.ip-address}")
	private String ip ;
	
	@Value("${server.port}")
	private int port ;
//
//	@Autowired
//	private HttpServletRequest request ;
//
//	@Autowired
//	private HttpServletResponse response ;
//	DefaultHttpRequest(decodeResult: success, version: HTTP/1.1)
//	GET /v2/api-docs HTTP/1.1
//	Pragma: no-cache
//	Cache-Control: no-cache
//	Accept: application/json,*/*
//	User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
//	Referer: http://192.168.116.117:8098/swagger-ui.html?urls.primaryName=file-server
//	Accept-Encoding: gzip, deflate
//	Accept-Language: zh-CN,zh;q=0.9
//	Forwarded: proto=http;host="192.168.116.117:8098";for="192.168.116.117:63714"
//	X-Forwarded-For: 192.168.116.117
//	X-Forwarded-Proto: http
//	X-Forwarded-Prefix: /file-server
//	X-Forwarded-Port: 8098
//	X-Forwarded-Host: 192.168.116.117:8098
//	host: 192.168.116.117:9005
//	@RequestMapping(method= RequestMethod.GET,value="/v2/api-docs")
//	public void getApi() throws IOException {
//		Resource resource = resourceLoader.getResource("classpath:api/api.json") ;
//		InputStream inputStream = null;
//		BufferedReader bufferedReader = null;
//		StringBuilder builder = new StringBuilder();
//		try {
//			inputStream = resource.getInputStream();
//			bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
//			String str = null;
//			while ((str = bufferedReader.readLine()) != null) {
//				str = str.trim();
//				if (!str.equals("")) {
//					builder.append(str);
//				}
//			}
//			String string = builder.toString();
//			log.info(string);
//			String host = request.getHeader("X-Forwarded-Host");
//
//			String pre = request.getHeader("X-Forwarded-Prefix");
//
//			string = string.replace("\"host\":\"replaceAll\"","\"host\":\""+host+pre+"\"");
//			response.setResponse(string);
//		}finally {
//			IOUtils.closeQuietly(inputStream);
//		}
//
//	}
}
